package com.wn.atm;

import java.math.BigInteger;
import java.security.MessageDigest;

public class User
{

	/**
	 * 蜗牛账户
	 */
	private String name;
	private String password;
	private double money;

	// 构造
	public User()
	{
		this.name = null;
		this.password = null;
		this.money = 0;
	}

	// 带参构造,从文件读取时候使用
	public User(String name, String password, double money)
	{
		this.name = name;
		this.password = password;//
		this.money = money;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	// 不可直接获取密码
	public String getPassword()
	{
		return password;
	}

	// 核对密码
	public boolean checkPassword(String password)
	{
		if (getMD5Str(password).equals(this.password))
		{
			return true;
		}
		return false;
	}

	public void setPassword(String password)
	{
		try{
			password=getMD5Str(password);
		}
		catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}	
		this.password = password;
	}

	/*
	 * md5加密
	 */
	public static String getMD5Str(String str)
	{
		try
		{
			// 生成一个MD5加密计算摘要
			MessageDigest md = MessageDigest.getInstance("MD5");
			// 计算md5函数
			md.update(str.getBytes());
			// digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
			// BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
			return new BigInteger(1, md.digest()).toString(16);
		} catch (Exception e)
		{
			System.out.println("MD5加密出现错误，" + e.toString());
			return "";
		}

	}

	
	public double getMoney()
	{
		return money;
	}

	public void setMoney(double money)
	{
		this.money = money;
	}

}
